<?php

namespace App\Models\SystemManagement;

use App\Models\CommonModel;
use App\Models\Constant\Fields;

class Post extends CommonModel
{
    protected $table = 'posts';
    public $primaryKey = 'id';

    protected $hidden = ['deleted_at','created_at','updated_at'];

    /**
     * @param $data
     * @return string
     * 添加岗位数据
     */
    public static function addData($data){
        //保存数据
        $param = new self();
        $res = $param->create($data);
        return $res;
    }

    /**
     * @param $per
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     * 岗位列表 带分页
     */
    public static function getPageData($request){
        $per_page = $request->input('per_page');
        return self::with(['organization','department','postSkill'])
                ->orderBy(Fields::ID)
                ->where(function($query) use ($request){
                    $organization_id = $request->input('organization_id','');
                    $department_id = $request->input('department_id');
                    if(!empty($organization_id)) {
                        $query->where('organization_id',$organization_id);
                    }
                    if(!empty($department_id)) {
                        $query->where('department_id',$department_id);
                    }
                })
                ->paginate($per_page);
    }

    /**
     * @param $id
     * @return mixed
     * 获取单条岗位数据
     */
    public static function getOne($id){
        $query = self::find($id);
        return $query;
    }

    /**
     * @param $con
     * @return mixed
     * 通过条件查找
     */
    public static function getByCondition($con){
        $query = self::getTableDataByCondition($con,new self());
        return $query;
    }

    /**
     * @param $arr
     * @return bool
     * 修改某条数据
     */
    public static function updateData($arr,$id){
        $query = self::updateTableData($arr,$id,new self());
        return $query;
    }

    /**
     * @param $ids
     * @return \App\Models\Error
     * 删除
     */
    static function del($ids){
        $query = self::batchDelTableData($ids,new self());
        return $query;
    }

    /**
     * @param $ids
     * @return \App\Models\Error
     * 恢复
     */
    static function recover($ids){
        $query = self::batchResetTableData($ids,new self());
        return $query;
    }

    //获取工作量数据
    public function job()
    {
        return $this->belongsTo(\App\Models\Landscaping\ParkJob::class,'id','post_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * 岗位所属部门
     */
    public function department(){
        return $this->belongsTo(self::$SYSTEM.'\Department',Fields::DEPART_ID,Fields::ID)->select('id',Fields::Name);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * 岗位使用组织
     */
    public function organization(){
        return $this->belongsTo(self::$SYSTEM.'\Organization',Fields::ORGAN_ID,Fields::ID)->select('id',Fields::Name);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * 岗位职级关系
     */
    public function postSkill(){
        return $this->hasMany(self::$SYSTEM.'\PostSkill',Fields::POST_ID,Fields::ID)->with('skill');
    }

}
